﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DAL.DataOperate;

namespace BLL.Business
{
    public class BookBLL
    {
        DAL.DataOperate.BookDAL bookDAL = new BookDAL();

        #region 选择
        public DataSet SelectAll()
        {
            return bookDAL.SelectAll();
        }
        public DataSet SelectByBookId(int BookId)
        {
            if (BookId.ToString() == null || BookId.ToString() == "")
                return null;
            else
                return bookDAL.SelectByBookId(BookId);
        }
        public DataSet SelectNewBook()
        {
            return bookDAL.SelectNewBook();
        }
        public DataSet SelectHotBook()
        {
            return bookDAL.SelectHotBook();
        }
        public DataSet SearchBook(String BookName, String BookTypeName, float beginPrice, float endPrice)
        {
            return bookDAL.SearchBook(BookName, BookTypeName, beginPrice, endPrice);
        }
        #endregion

        #region 增、删、改
        public bool InsertBook(Model.Book book)
        {
            if (book.Book_Namestr == "" || book.SmallType_Namestr == "" ||
                book.Book_Authorstr == "" || book.Book_Publisherstr == "" ||
                book.Book_Priceflo.ToString() == "" || book.Book_Descriptiontext == "" ||
                book.Book_Countint.ToString() == "" || book.Book_Statusbit.ToString() == "" ||
                book.Book_ImportTimedate.ToString() == "" || book.Book_Coverstr == "" ||
                book.Book_ImportCountint.ToString() == "")
            {
                return false;
            }
            else
            {
                DataSet ds = new DataSet();
                ds = SearchBook(book.Book_Namestr, book.SmallType_Namestr, 0, float.MaxValue);
                if ((ds == null) ||
                    (ds.Tables.Count == 0) ||
                    (ds.Tables.Count == 1 && ds.Tables[0].Rows.Count == 0))
                {
                    return bookDAL.InserBook(book);
                }
                else
                {
                    return false;
                }
            }
        }
        public bool UpdateBook(Model.Book book)
        {
            if (book.Book_Idint.ToString() == "" || book.Book_Namestr == "" ||
                book.SmallType_Namestr == "" || book.Book_Authorstr == "" ||
                book.Book_Publisherstr == "" || book.Book_Priceflo.ToString() == "" ||
                book.Book_Descriptiontext == "" || book.Book_Countint.ToString() == "" ||
                book.Book_Statusbit.ToString() == "" || book.Book_ImportTimedate.ToString() == "" || 
                book.Book_Coverstr == "" || book.Book_ImportCountint.ToString() == "")
                return false;
            else
            {
                return bookDAL.UpdateBook(book);
            }
        }
        public bool UpdateBook(int bookId, int buyCount)
        {
            if ((bookId.ToString() == "" || bookId.ToString() == null) ||
               (buyCount.ToString() == "" || buyCount.ToString() == null))
            {
                return false;
            }
            else
            {
                return bookDAL.UpdateBook(bookId, buyCount);
            }
        }
        public bool DeleteBook(int bookId)
        {
            if (bookId.ToString() == "" || bookId.ToString() == null)
            {
                return false;
            }
            else
            {
                return bookDAL.DeleteBook(bookId);
            }
        }
        #endregion

        #region 操作分类

        #region 大类操作
        public DataSet SelectBigType()
        {
            return bookDAL.SelectBigType();
        }
        public bool InsertBig(String BigTypeName)
        {
            if (BigTypeName.ToString() == "" || BigTypeName.ToString() == null)
            {
                return false;
            }
            else
            {
                DataSet ds = new DataSet();
                ds = bookDAL.SelectBigType(BigTypeName);
                if ((ds == null) ||
                   (ds.Tables.Count == 0) ||
                   (ds.Tables.Count == 1 && ds.Tables[0].Rows.Count == 0))
                {
                    return bookDAL.InsertBig(BigTypeName);
                }
                else
                {
                    return false;
                }
            }
        }
        public bool UpdateBig(int BigTypeId, String BigTypeName)
        {
            if (BigTypeId.ToString() == "" || BigTypeId.ToString() == null ||
                BigTypeName.ToString() == "" || BigTypeName.ToString() == null)
            {
                return false;
            }
            else
            {
                return bookDAL.UpdateBig(BigTypeId, BigTypeName);
            }
        }
        public bool DeleteBig(int BigTypeId)
        {
            if (BigTypeId.ToString() == "" || BigTypeId.ToString() == null)
            {
                return false;
            }
            else
            {
                return bookDAL.DeleteBig(BigTypeId);
            }
        }
        #endregion

        #region 小类操作
        public DataSet SelectSmallType()
        {
            return bookDAL.SelectSmallType();
        }
        public DataSet SelectSmallType(String BigTypeName)
        {
            if (BigTypeName == "" || BigTypeName == null)
                return null;
            else
                return bookDAL.SelectSmallType(BigTypeName);
        }
        public DataSet SelectSmallType(int BigTypeId)
        {
            if (BigTypeId.ToString() == null || BigTypeId.ToString() == "")
            {
                return null;
            }
            else
            {
                return bookDAL.SelectSmallType(BigTypeId);
            }
        }
        public bool InsertSmall(String BigTypeName, String SmallTypeName)
        {
            if (BigTypeName == "" || BigTypeName == null ||
                SmallTypeName == "" || SmallTypeName == null)
            {
                return false;
            }
            else
            {
                DataSet ds = new DataSet();
                ds = bookDAL.SelectSmallType(BigTypeName, SmallTypeName);
                if ((ds == null) ||
                  (ds.Tables.Count == 0) ||
                  (ds.Tables.Count == 1 && ds.Tables[0].Rows.Count == 0))
                {
                    return bookDAL.InsertSmall(BigTypeName, SmallTypeName);
                }
                else
                {
                    return false;
                }
            }
        }
        public bool UpdateSmall(int SmallTypeId, String BigTypeName, String SmallTypeName)
        {
            if (SmallTypeId.ToString() == "" || SmallTypeId.ToString() == null ||
                BigTypeName == "" || BigTypeName == null ||
                SmallTypeName == "" || SmallTypeName == null)
            {
                return false;
            }
            else
            {
                return bookDAL.UpdateSmall(SmallTypeId,  BigTypeName,  SmallTypeName);
            }
        }
        public bool DeleteSmall(int SmallTypeId)
        {
            if (SmallTypeId.ToString() == "" || SmallTypeId.ToString() == null)
            {
                return false;
            }
            else
            {
                return bookDAL.DeleteSmall(SmallTypeId);
            }
        }
        #endregion

        #endregion

        #region 二手书操作区
        public DataSet SelectSecondBookByUserId(int UserId)
        {
            if (UserId.ToString() == "")
            {
                return null;
            }
            else
            {
                return bookDAL.SelectSecondBookByUserId(UserId);
            }
        }
        public bool InsertSecondBook(Model.Book book, int UserId)
        {
            if (book.Book_Namestr == "" || book.SmallType_Namestr == "" ||
               book.Book_Authorstr == "" || book.Book_Publisherstr == "" ||
               book.Book_Priceflo.ToString() == "" || book.Book_Descriptiontext == "" ||
               book.Book_Countint.ToString() == "" || book.Book_Statusbit.ToString() == "" ||
               book.Book_ImportTimedate.ToString() == "" || book.Book_Coverstr == "" ||
               book.Book_ImportCountint.ToString() == "" || UserId.ToString() == "")
            {
                return false;
            }
            else
            {
                DataSet ds = new DataSet();
                ds = SearchBook(book.Book_Namestr, book.SmallType_Namestr, 0, float.MaxValue);
                if ((ds == null) ||
                    (ds.Tables.Count == 0) ||
                    (ds.Tables.Count == 1 && ds.Tables[0].Rows.Count == 0))
                {
                    return bookDAL.InsertSecondBook(book,UserId);
                }
                else
                {
                    return false;
                }
            }
        }
        #endregion
    }
}
